package com.itmake.sample.toolbox.file;

import java.text.DecimalFormat;

/**
 * 文件工具类
 *
 * @author Amjacks
 * @since 2023/08/10
 */
public class FileUtil {

    private FileUtil() {}

    /**
     * 格式化文件大小
     * @param fileS 文件长度
     */
    public static String formatFileSize(long fileS) {
        DecimalFormat df = new DecimalFormat("#.00");
        String fileSizeString = "";
        String wrongSize = "0B";
        if (fileS == 0) {
            return wrongSize;
        }
        if (fileS < 1024) {
            fileSizeString = df.format((double) fileS) + " B";
        } else if (fileS < 1048576) {
            fileSizeString = df.format((double) fileS / 1024) + " KB";
        } else if (fileS < 1073741824) {
            fileSizeString = df.format((double) fileS / 1048576) + " MB";
        } else {
            fileSizeString = df.format((double) fileS / 1073741824) + " GB";
        }
        return fileSizeString;
    }
}
